/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.cred.industries.platform.resources;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cred.industries.core.exceptions.ApplicationException;
import com.cred.industries.core.util.ExceptionResponse;
import com.cred.industries.platform.business.facade.MetricsFacade;
import com.cred.industries.platform.defines.Role;

@Path("/metrics")
public class MetricsRes {
	private static final Logger logger = LoggerFactory.getLogger(MetricsRes.class);
	
	@POST
	@Path("/secondsInApp")
	@RolesAllowed({Role.ADMIN_STRING, Role.CS_STRING, Role.USER_STRING})
	public Response timeSpentViewingSlide(@DefaultValue("0") @QueryParam("secondsInApp") int secondsInApp) {
		
		try {
			
			MetricsFacade metricsF = new MetricsFacade();
			metricsF.timeSpentInApp(secondsInApp);
			return Response.ok().build();
			
		} catch (ApplicationException | WebApplicationException e) {
			logger.error("Exception " + e.toString() + " " + e.getMessage());
			e.printStackTrace();
			throw e;
		} catch (Throwable e) {
			logger.error("Exception " + e.toString() + " " + e.getMessage());
			e.printStackTrace();
			throw new WebApplicationException(ExceptionResponse.build(e));
		} 
	}
}
